﻿using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using System.Collections.Generic;

using Styx;
using Styx.Plugins;
using Styx.Common;
using Styx.CommonBot;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace rndbuddy
{
    static class crowdsourcer
    {
        public static NameValueCollection nodepool = new NameValueCollection();

        public static void get_nodes()
        {
            if (!settings.enable_crowdsourcer)
                return;

            try
            {
                var NodeList = (from o in ObjectManager.GetObjectsOfType<WoWGameObject>(false, false)
                                where (
                                        (o.IsHerb || o.IsMineral)
                                        &&
                                        (o.Distance2D <= settings.crowdsourcer_collect_2d_distance)
                                        &&
                                        (!o.IsDisabled)
                                    ) 
                                orderby o.DistanceSqr ascending
                                select o).ToList();

                foreach (WoWGameObject node in NodeList)
                {
                    if (node == null) 
                        continue;

                    string isIndoor = "0";
                    if (node.IsIndoors)
                        isIndoor = "1";

                    if (StyxWoW.Me.ZoneId == 0)
                    {
                        rndbuddy.logsilent("not collecting Hotspot. ZoneID equals 0");
                        return;
                    }

                    string data = StyxWoW.Me.MapId + ";" +
                                  StyxWoW.Me.ZoneId + ";" +
                                  StyxWoW.Me.ZoneText + ";" +
                                  node.Name + ";" +
                                  node.DisplayId.ToString() + ";" +
                                  node.X.ToString() + ";" +
                                  node.Y.ToString() + ";" +
                                  node.Z.ToString() + ";" +
                                  isIndoor + ";" +
                                  StyxWoW.Me.SubZoneText;

                    string id = Crypto.GetHashString(data);
                    if (!is_stored(id))
                        nodepool.Add(id, data);
                }
            }
            catch (Exception e)
            {
                rndbuddy.logsilent("Exception thrown in crowdsoucer: " + e.Message);
                rndbuddy.logsilent("Stacktrace: " + e.StackTrace);
            }
        }

        public static string merge_and_report(bool reset_collection)
        {
            if (!settings.enable_crowdsourcer)
                return "DISABLED";

            var array = (from key in nodepool.AllKeys
                         from value in nodepool.GetValues(key)
                         select value).ToArray();

            //empty pool
            if (reset_collection)
                nodepool = new NameValueCollection();

            return (string.Join(";;;;", array));
        }

        public static bool is_stored(string id)
        {
            string value = nodepool[id];
            if (value == null)
                return false;
            return true;
        }

        public static int amount_of_nodes()
        {
            return nodepool.Count;
        }
    }
}
